package com.bang.study.scheduled;

import com.bang.study.constants.PublicConstants;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * @ClassName: ScheduledMessageConsumer
 * @Desc: TODO
 * @Auther: <Bang>
 * @Date: 2020/05/06 20:23
 * @Version: 1.0
 * @Modified By:
 */
public class ScheduledMessageConsumer {

    public static void main(String[] args) throws Exception{
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(PublicConstants.TEST_CONSUMER_GROUP);
        consumer.setNamesrvAddr(PublicConstants.NAME_ADDR);
        consumer.subscribe("Scheduled_Topic","*");
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                for (MessageExt message : msgs) {
                    // Print approximate delay time period
                    System.out.println("Receive message[msgId=（" + message.getMsgId() + "）] "
                            + (System.currentTimeMillis() - message.getStoreTimestamp()) + "ms later");
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
        System.out.println("消费者启动=================");
    }
}
